package com.sunny.client.v1;

import com.sunny.client.discover.IServiceDiscovery;
import com.sunny.service.RpcRequest;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author Sunny
 * @date 2020/2/6
 */
public class RpcProxyHandler implements InvocationHandler {
    private IServiceDiscovery serviceDiscovery;
    private String version;

    public RpcProxyHandler(IServiceDiscovery serviceDiscovery, String version) {
        this.serviceDiscovery = serviceDiscovery;
        this.version = version;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setParameters(args);
        rpcRequest.setVersion(version);

        RpcNetProcessHandler rpcNetProcessHandler = new RpcNetProcessHandler(rpcRequest);
        return rpcNetProcessHandler.invoke(serviceDiscovery);
    }
}
